from . import db
from sqlalchemy.types import JSON
from uuid import uuid4
from sqlalchemy import func, DateTime


class CareElderDemand(db.Model):
    """老人照护需求表"""
    __tablename__ = "care_elder_demand"
    id = db.Column(db.BigInteger, primary_key=True)
    elder = db.Column(db.String(255), nullable=False)
    elder_name = db.Column(db.String(255), nullable=True)
    gender = db.Column(db.Integer, nullable=True)
    time_slots = db.Column(db.String(50), nullable=True)
    actions = db.Column(db.String(50), nullable=True)
    caregiver = db.Column(db.String(255), nullable=True)
    arrange = db.Column(db.String(255), nullable=True)

    def __repr__(self):
        return f"<CareElderDemand {self.id} {self.elder} arrange:{self.arrange}>"


class CareGbNursingAction(db.Model):
    """护理动作表"""
    __tablename__ = "care_gb_nursing_action"
    id = db.Column(db.Integer, primary_key=True)
    pid = db.Column(db.Integer, nullable=True)
    name = db.Column(db.String(255), nullable=False)
    level = db.Column(db.Integer, nullable=True)
    nursing_id = db.Column(db.String(50), nullable=True)

    def __repr__(self):
        return f"<CareGbNursingAction {self.id} {self.name}>"


class DigitalEmployeeSkills(db.Model):
    """数字员工技能表"""
    __tablename__ = "digital_employee_skills"
    id = db.Column(db.BigInteger, primary_key=True)
    parent_id = db.Column(db.BigInteger, nullable=True)
    name = db.Column(db.String(255), nullable=False)
    level = db.Column(db.BigInteger, nullable=True)
    nursing_level = db.Column(db.String(50), nullable=True)
    sort = db.Column(db.String(20), nullable=True)
    create_dept = db.Column(db.BigInteger, nullable=True)
    create_by = db.Column(db.BigInteger, nullable=True)
    create_time = db.Column(db.DateTime, nullable=True)
    update_by = db.Column(db.BigInteger, nullable=True)
    update_time = db.Column(db.DateTime, nullable=True)
    remake = db.Column(db.String(255), nullable=True)

    def __repr__(self):
        return f"<DigitalEmployeeSkills {self.id} {self.name}>"


class AppUser(db.Model):
    """应用用户表"""
    __tablename__ = "app_user"
    user_id = db.Column(db.BigInteger, primary_key=True)
    tenant_id = db.Column(db.String(20), nullable=True)
    dept_id = db.Column(db.BigInteger, nullable=True)
    role_id = db.Column(db.BigInteger, nullable=True)
    user_name = db.Column(db.String(30), nullable=False)
    nick_name = db.Column(db.String(30), nullable=True)
    user_type = db.Column(db.String(10), nullable=True)
    email = db.Column(db.String(50), nullable=True)
    phone_number = db.Column(db.String(11), nullable=True)
    face_url = db.Column(db.String(255), nullable=True)
    sex = db.Column(db.String(1), nullable=True)
    avatar = db.Column(db.BigInteger, nullable=True)
    password = db.Column(db.String(500), nullable=True)
    salt = db.Column(db.String(255), nullable=True)
    system_type = db.Column(db.String(50), nullable=True)
    status = db.Column(db.String(1), nullable=True)
    del_flag = db.Column(db.String(1), nullable=True)
    login_ip = db.Column(db.String(128), nullable=True)
    login_date = db.Column(db.DateTime, nullable=True)
    create_dept = db.Column(db.BigInteger, nullable=True)
    create_by = db.Column(db.BigInteger, nullable=True)
    create_time = db.Column(db.DateTime, nullable=True)
    update_by = db.Column(db.BigInteger, nullable=True)
    update_time = db.Column(db.DateTime, nullable=True)
    remark = db.Column(db.String(500), nullable=True)
    task_id = db.Column(db.BigInteger, nullable=True)
    chat_identity = db.Column(db.String(2), nullable=True)

    def __repr__(self):
        return f"<AppUser {self.user_id} {self.user_name}>"


class AppUserDetail(db.Model):
    """应用用户详情表"""
    __tablename__ = "app_user_detail"
    detail_id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.String(50), nullable=True)
    app_user_name = db.Column(db.String(50), nullable=False)
    post_id = db.Column(db.BigInteger, nullable=True)
    age = db.Column(db.Integer, nullable=True)
    id_card = db.Column(db.String(20), nullable=True)
    origin = db.Column(db.String(255), nullable=True)
    birthday = db.Column(db.DateTime, nullable=True)
    marriage = db.Column(db.Integer, nullable=True)
    ethnic = db.Column(db.String(10), nullable=True)
    colleges = db.Column(db.String(255), nullable=True)
    specialized = db.Column(db.String(255), nullable=True)
    degree = db.Column(db.String(255), nullable=True)
    skill = db.Column(db.Text, nullable=True)
    work_exp = db.Column(db.String(255), nullable=True)
    emergency_contacts = db.Column(db.String(255), nullable=True)
    contacts_phone = db.Column(db.String(255), nullable=True)
    status = db.Column(db.String(1), nullable=True)
    del_flag = db.Column(db.String(1), nullable=True)
    create_dept = db.Column(db.BigInteger, nullable=True)
    create_by = db.Column(db.BigInteger, nullable=True)
    create_time = db.Column(db.DateTime, nullable=True)
    update_by = db.Column(db.BigInteger, nullable=True)
    update_time = db.Column(db.DateTime, nullable=True)
    remark = db.Column(db.String(500), nullable=True)

    def __repr__(self):
        return f"<AppUserDetail {self.detail_id} {self.name}>"


class CareScheduleResultOld(db.Model):
    """照护计划总表"""
    __tablename__ = "care_schedule_results_old"
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid4()))
    elder = db.Column(db.String(36), nullable=False)
    schedule_date = db.Column(DateTime, nullable=False)
    schedule_data = db.Column(JSON, nullable=False)
    plan_date = db.Column(db.Date, nullable=True, index=True)
    batch_number = db.Column(db.Integer, nullable=True, index=True)
    create_time = db.Column(db.DateTime, nullable=True, default=func.now())
    update_time = db.Column(db.DateTime, nullable=True, default=func.now(), onupdate=func.now())
    status = db.Column(db.String(20), nullable=True, default="active")

    def __repr__(self):
        return f"<CareScheduleResultOld {self.id} {self.elder} {self.schedule_date}>"


class CareTimeSlotOld(db.Model):
    """照护计划分表"""
    __tablename__ = "care_time_slots_old"
    id = db.Column(db.String(36), primary_key=True, default=lambda: str(uuid4()))
    schedule_result_id = db.Column(db.String(36), db.ForeignKey("care_schedule_results_old.id"), nullable=False)
    elder = db.Column(db.String(36), nullable=False)
    time_slot = db.Column(db.String(11), nullable=False)
    activity = db.Column(db.String(100), nullable=False)
    activity_id = db.Column(db.String(100), nullable=False)
    caregiver = db.Column(db.String(100), nullable=False)
    caregiver_id = db.Column(db.String(100), nullable=False)
    time_order = db.Column(db.Integer, nullable=False)
    schedule_date = db.Column(DateTime, nullable=False)
    plan_date = db.Column(db.Date, nullable=True, index=True)
    batch_number = db.Column(db.Integer, nullable=True, index=True)
    create_time = db.Column(db.DateTime, nullable=True, default=func.now())
    update_time = db.Column(db.DateTime, nullable=True, default=func.now(), onupdate=func.now())
    status = db.Column(db.String(20), nullable=True, default="active")

    def __repr__(self):
        return f"<CareTimeSlotOld {self.id} {self.elder} {self.time_slot} {self.activity}>"

    @classmethod
    def get_ordered_slots(cls):
        return cls.query.order_by(
            cls.schedule_result_id,
            cls.elder,
            cls.schedule_date,
            func.substr(cls.time_slot, 1, 2),
            func.substr(cls.time_slot, 4, 2),
        ).all()
